{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "owned-rough",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36msalaries\u001b[39m: \u001b[32mSeq\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m100000\u001b[39m, \u001b[32m200000\u001b[39m, \u001b[32m300000\u001b[39m)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val salaries = Seq(100000,200000,300000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "unusual-explosion",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mdoubleSalary\u001b[39m: \u001b[32mInt\u001b[39m => \u001b[32mInt\u001b[39m = <function1>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val doubleSalary = (x :Int) => x*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "boxed-theology",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mnewSalaries\u001b[39m: \u001b[32mSeq\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m200000\u001b[39m, \u001b[32m400000\u001b[39m, \u001b[32m600000\u001b[39m)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val newSalaries1 = salaries.map(doubleSalary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "lonely-concrete",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mnewSalaries2\u001b[39m: \u001b[32mSeq\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m300000\u001b[39m, \u001b[32m600000\u001b[39m, \u001b[32m900000\u001b[39m)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val newSalaries2 = salaries.map(x => x * 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "balanced-strain",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mnewSalaries3\u001b[39m: \u001b[32mSeq\u001b[39m[\u001b[32mInt\u001b[39m] = \u001b[33mList\u001b[39m(\u001b[32m500000\u001b[39m, \u001b[32m1000000\u001b[39m, \u001b[32m1500000\u001b[39m)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val newSalaries3 = salaries.map(_ *5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "important-plaza",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mWeeklyWeather\u001b[39m"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "case class WeeklyWeather (temperatures:Seq[Double]){\n",
    "    private def convertCtoF(temp: Double) = temp *1.8 +32\n",
    "    def forecastIntFahrenheit :Seq[Double] = temperatures.map(convertCtoF)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "designing-connection",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mobject\u001b[39m \u001b[36mSalaryRaiser\u001b[39m"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "object SalaryRaiser {\n",
    "\n",
    "  private def promotion(salaries: List[Double], promotionFunction: Double => Double): List[Double] =\n",
    "    salaries.map(promotionFunction)\n",
    "\n",
    "  def smallPromotion(salaries: List[Double]): List[Double] =\n",
    "    promotion(salaries, salary => salary * 1.1)\n",
    "\n",
    "  def bigPromotion(salaries: List[Double]): List[Double] =\n",
    "    promotion(salaries, salary => salary * math.log(salary))\n",
    "\n",
    "  def hugePromotion(salaries: List[Double]): List[Double] =\n",
    "    promotion(salaries, salary => salary * salary)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fiscal-saver",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.example.com/users?id=1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36murlBuilder\u001b[39m\r\n",
       "\u001b[36mdomainName\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"www.example.com\"\u001b[39m\r\n",
       "\u001b[36mgetURL\u001b[39m: (\u001b[32mString\u001b[39m, \u001b[32mString\u001b[39m) => \u001b[32mString\u001b[39m = <function2>\r\n",
       "\u001b[36mendpoint\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"users\"\u001b[39m\r\n",
       "\u001b[36mquery\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"id=1\"\u001b[39m\r\n",
       "\u001b[36murl\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"https://www.example.com/users?id=1\"\u001b[39m"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def urlBuilder(ssl:Boolean,domainName:String):(String,String) => String = {\n",
    "    val schema = if (ssl) \"https://\" else \"http://\"\n",
    "    (endpoint:String,query :String) => s\"$schema$domainName/$endpoint?$query\"\n",
    "}\n",
    "val domainName = \"www.example.com\"\n",
    "val getURL = urlBuilder(ssl=true, domainName)//用def也可以，用val也可以，\n",
    "val endpoint = \"users\"\n",
    "val query = \"id=1\"\n",
    "val url = getURL(endpoint, query)\n",
    "println (url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fifth-liberal",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Factorial of 2: 2\n",
      "Factorial of 3: 6\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36mfactorial\u001b[39m"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def factorial(x:Int):Int ={\n",
    "    def fact(x:Int,acc:Int):Int = {\n",
    "        if (x<=1) acc\n",
    "        else fact(x - 1,x*acc)\n",
    "    }\n",
    "    fact(x,1)\n",
    "}\n",
    " println(\"Factorial of 2: \" + factorial(2))\n",
    " println(\"Factorial of 3: \" + factorial(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "devoted-lying",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
